var express = require('express');
var router = express.Router();
var db = require('../core/mysql.js');
var util = require('util');
var mysql = require('mysql');
var cookie = require('cookie');
var parse = require('../public/javascripts/parse.js');

/* GET users listing. */
router.get('/reg', function(req, res, next) {
  res.render('users/reg.art' ,{title:'用户注册'});
});
router.get('/login', function(req, res, next) {
  res.render('users/login.art' ,{title:'用户登录'});
});
router.get('/loginOut', function(req, res, next) {
  // req.session.user = '';
  res.clearCookie('user');
  res.redirect('/index' );
});
router.post('/addUser', function(req, res, next) {
	// 新增用户
  var userName = req.body.userName;
  if(!userName){
  	 req.flash('error','用户名不能为空');
  	 res.redirect('./reg');
  	 return ;
  }
  if(!req.body.password){
  	 req.flash('error','密码不能为空');
  	 res.redirect('./reg');
  	 return ;
  }
  queryUser({userName:userName},function(err,result){
  	  if(result.length>0){
  	  	req.flash('error','用户名已存在');
  	  	res.redirect('./reg');
  	  	return ;
  	  }
	  var sql = "insert into blog_user(user_name,password,show_name) values (:userName,:password,:showName)";
	  db.query(sql,{userName:userName,password:req.body.password,showName:req.body.showName},function(err,addRes){
	  		if(!err){
          req.flash('success','注册成功');
          res.redirect('/index');
	  		}
	  });
  });
});
router.post('/login', function(req, res, next) {
	// 新增用户
  var userName = req.body.userName;
  if(!userName){
     req.flash('error','用户名为空');
  	 res.redirect('./login');
  	 return ;
  }
  if(!req.body.password){
  	 req.flash('error','密码为空');
  	 res.redirect('./login');
  	 return ;
  }
  queryUser({userName:userName,password:req.body.password},function(err,result){
  	  if(result.length===0){
  	  	req.flash('error','用户名或密码错误');
  	  	res.redirect('./login');
  	  	return ;
  	  }
      res.cookie('user', result[0]);
  	  // req.session.user = result[0];
  	  // req.flash('success','登录成功');
      res.redirect('/index');
  	  // res.render('index');
  });
});

function queryUser(user={},callback){
	var sql = "select id , user_name userName,password ,show_name showName from blog_user where 1";
	var queryObj = {};
	if(user.id){
		sql += " and id=:id";
		queryObj.id = user.id;
	}
	if(user.userName){
		sql += " and user_name=:userName";
		queryObj.userName = user.userName;
	}	
	if(user.password){
		sql += " and password=:password";
		queryObj.password = user.password;
	}
	if(user.showNameLike){
		sql += " and show_name like %:showNameLike%";
		queryObj.showNameLike = user.showNameLike;
	}
	db.query(sql,queryObj,function(err,result){
		if(err){
			console.error(err);
			return;
		}
		callback(err,result);
	});
}

router.get('/my/:username', function(req, res, next) {
  res.send('user: ' + req.params.username);
});

router.get('/', function(req, res, next) {
  req.cookies = parse.JSONCookies(cookie.parse(req.headers.cookie)); 
  var user = req.cookies.user;
	if(user){
  	res.send('user: ' + util.inspect(user.showName));
		return;
	}
  res.send('respond with a resource');
});

module.exports = router;
